package top.lixunda.ecommerce.server.user.feign;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.user.api.entity.db.AccountDO;
import top.lixunda.ecommerce.server.user.api.entity.db.UserDetailDO;
import top.lixunda.ecommerce.server.user.api.entity.dto.AccountQueryDTO;
import top.lixunda.ecommerce.server.user.api.feign.IFeignAccountController;
import top.lixunda.ecommerce.server.user.service.IAccountService;

import java.util.List;

/**
 * 用户相关内部调用接口，需要内部权限限定
 *
 * @author lixunda
 * @version time:2020/4/14 15:30
 */
@Slf4j
@RestController
public class FeignAccountController extends BaseController
        implements IFeignAccountController {

    private final IAccountService accountService;

    @Autowired
    public FeignAccountController(IAccountService accountService) {
        this.accountService = accountService;
    }

    /**
     * 根据id查询账户数据
     *
     * @param accountId 账户编号
     * @return 账户信息
     */
    @Override
    public MessageObject<AccountDO> queryAccountById(String accountId) {
        AccountDO accountDO = accountService.queryAccountById(accountId);
        return getSuccessMessageObject("获取账户信息成功", accountDO);
    }

    /**
     * 条件查询账户数据
     *
     * @param accountQueryDTO 条件查询信息
     * @return 账户列表
     */
    @Override
    public MessageObject<List<AccountDO>> queryAccountByWrapper(AccountQueryDTO accountQueryDTO) {
        List<AccountDO> result = accountService.queryAccountByWrapper(accountQueryDTO);
        return getSuccessMessageObject("获取账户信息成功", result);
    }

    /**
     * 分页条件查询用户数据
     *
     * @param pageDTO 添加分页数据
     * @return 账户分页数据
     */
    @Override
    public MessageObject<Page<AccountDO>> queryAccountByPageWrapper(PageDTO<AccountDO, AccountQueryDTO> pageDTO) {
        IPage<AccountDO> result = accountService.queryAccountByPageWrapper(pageDTO);
        return getSuccessMessageObject("获取账户信息成功", (Page<AccountDO>) result);
    }

    /**
     * 使用token进行登录
     *
     * @param token token字符串
     * @return 返回去除密码账户信息，登陆失败时返回Object为Null
     */
    @Override
    public MessageObject<UserDetailDO> loginByTokenString(String token) {
        UserDetailDO userDetailDO = accountService.loginByTokenString(token);
        return getSuccessMessageObject("登陆成功", userDetailDO);
    }
}
